package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private final int LIST_BOOKS_OPTION = 0;
    private final int RESERVE_OPTION = 1;
    private final int CHECK_OPTION = 2;
    private final int LIST_MOVIES_OPTION = 3;

    private String[] options = new String[]{"List all books in the library", "Reserve book for collection",
            "Check membership details", "List all movies in the library"};

    private List<Book> books;
    private List<Movie> movies;
    private List<User> users;

    private User userLoggedIn;

    public BibliotecaApp() {
        this.books = new ArrayList<Book>();
        this.movies = new ArrayList<Movie>();
        this.users = new ArrayList<User>();
        populateBooks();
        populateMovies();
        populateUsers();
    }

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        bibliotecaApp.console();
    }

    public String start() {
        return "Welcome to Biblioteca!";
    }

    public String menu() {
        String menu = "";
        for (int i = 0; i < options.length; i++) {
            menu += i + " - " + options[i]+"\n";
        }
        return menu;
    }

    public String select(int option) {
        if (option < 0 || option >= options.length) {
            return "Select a valid option!!";
        }
        switch (option) {
            case CHECK_OPTION:
                return doCheck();
            case LIST_MOVIES_OPTION:
                return listAllMovies();
            case RESERVE_OPTION:
                return doReserve();
            default:
                return listAllBooks();
        }
    }

    private String doCheck() {
        if (userLoggedIn()) {
            return userLoggedIn.toString();
        }
        return "Please talk to a Librarian. Thank you.";
    }

    private String doReserve() {
        if (!userLoggedIn()) {
            return "Please login first!";
        }
        return listAllBooks();
    }

    public String listAllBooks() {
        String listOfBooks = "";
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            listOfBooks += i + " " + book.toString() + "\n";
        }
        return listOfBooks;
    }

    public String listAllMovies() {
        String format = "|%1$-20s|%2$-20s|%3$-5s|%4$-2s\n";
        StringBuilder listMovieLabel = new StringBuilder();
        listMovieLabel.append(String.format(format, "Movie", "Director", "Year", "Rating"));
        String listOfMovies = listMovieLabel.toString()+"\n";

        for (Movie movie : movies) {
            listOfMovies += movie.toString() + "\n";
        }
        return listOfMovies;
    }

    public String reserve(int bookIndex) {
        if (bookIndex < 0 || bookIndex >= books.size()) {
            return "Select a valid book!!";
        }
        if (!userLoggedIn()) {
            return "Please login first!";
        }
        Book book = books.get(bookIndex);
        if (!book.isAvailable()) {
            return "Sorry we don't have that book yet.";
        }
        book.setAvailable(false);
        return "Thank You! Enjoy the book.";
    }

    public String login(String login, String password) {
        for (User user : users) {
            if (user.equals(new User(login, password))) {
                userLoggedIn = user;
                return "Login Successfull!";
            }
        }
        return "Incorrect Login/Password!";
    }

    public boolean userLoggedIn() {
        return userLoggedIn != null;
    }

    public void console() {
        System.out.println(start());
        System.out.println(menu());
        Scanner keyboard = new Scanner(System.in);
        while (keyboard.hasNext()) {
            if (keyboard.hasNextInt()) {
                int option = keyboard.nextInt();
                System.out.println(select(option));
                if (option != RESERVE_OPTION) {
                    System.out.println(menu());
                } else {
                    System.out.print("Choose your book: ");
                    if (keyboard.hasNextInt()) {
                        int book = keyboard.nextInt();
                        System.out.println(reserve(book));
                        System.out.println(menu());
                    }
                }
            }
        }
    }

    private void populateBooks() {
        addBook("When Nietzsche Wept", "Irvin D Yalom");
        addBook("The Casual Vacancy", "J.K. Rowling");
        addBook("Curtain", "Agatha Christie");
        addBook("Nineteen Eighty Four", "George Orwell");
        addBook("The Solitaire Mystery", "Jostein Gaarder");
    }

    private void addBook(String title, String author) {
        this.books.add(new Book(title,author));
    }

    private void populateMovies() {
        addMovie("The Dark Knight","Christopher Nolan","2008","8");
        addMovie("The Godfather","Francis Ford Coppola","1972","9");
        addMovie("Schindler's List","Steven Spielberg","1993","8");
        addMovie("Fight Club","David Fincher","1999","8");
        addMovie("Before Sunrise","","1995","7");
        addMovie("Epic Movie","Jason Friedberg","2007","2");
        addMovie("Pulp Fiction","Quentin Tarantino","1994","9");
        addMovie("Inception","Christopher Nolan","2010","8");
        addMovie("Star Wars","George Lucas","1977","N/A");
        addMovie("The Wizard of Oz","Victor Fleming","1939","8");
        addMovie("The Intouchables","Olivier Nakache","2011","8");
        addMovie("Goodfellas","Martin Scorsese","1990","N/A");
        addMovie("Toy Story","John Lasseter","1995","8");
        addMovie("Snatch","Guy Ritchie","2000","8");
        addMovie("The Lion King","Roger Allers","1994","9");
    }

    private void addMovie(String title, String director, String year, String rating){
        this.movies.add(new Movie(title, director, year, rating));
    }

    private void populateUsers() {
        addUser("Dexter Morgan", "dmorgan@metropolice.com", "111-1111", "pass1", "90738212");
        addUser("Meredith Grey", "meredithg@seattlehospital.com", "111-1112", "pass2", "88740923");
        addUser("Ted Mosby", "ted@gmail.com", "111-1113", "pass3", "8762091");
        addUser("Jessica Day", "jess@gmail.com", "111-1114", "pass4", "334980321");
    }

    private void addUser(String name, String email, String login, String password, String phone) {
        this.users.add(new User(name, email, login, password, phone));
    }
}
